<?php

namespace app\common\logic;

use think\Model;

class RecordSumToday extends Model
{
    protected $table = 'sys_v_count_today';

    /**
     * 返回当天所有人的工作状态
     * @param array $where
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getAllWorkInfoToday($where = [])
    {
        $list = self::where($where)->order('code,id')->select();

        $temp = [];
        foreach ($list as $k => $v) {
//            $status = self::workStatus($v['count'], $v['dept']);
//            $list[$k]['status'] = ($status == '学习中' || $status == '工作中') ? 'working' : '';
//            $list[$k]['status_desc'] = $status;

            $status = '';
            if ($v['count'] == 0) $status = '休假中';
            else {
                if ($v['count'] % 2 == 0) $status = '休息中';
                else if ($v['count'] % 2 == 1) {
                    if (!empty($v['extattr'])) {
                        $extattr = unserialize($v['extattr']);
                        if (!isset($extattr['attrs']) || empty($extattr['attrs'])) {
                            $status = '工作中';
                        } else {
                            foreach ($extattr['attrs'] as $val) {
                                if ($val['name'] == '身份' && $val['value'] == '学生') $status = '学习中';
                            }
                        }
                    } else $status = '工作中';
                }
            }

            $list[$k]['status'] = ($status == '学习中' || $status == '工作中') ? 'working' : '';
            $list[$k]['status_desc'] = $status;
            $temp[$v['dept']][] = $list[$k];
        }

        $ret = [];
        foreach ($temp as $k => $v) {
            $ret[] = ['name' => $k, 'members' => $v];
        }

        return $ret;
    }

    /**
     * 工作状态
     * @param $count
     * @param $depart_name
     * @return string
     * @author wb <pithyone@vip.qq.com>
     */
    private static function workStatus($count, $depart_name)
    {
        $pid = Department::where(['name' => $depart_name])->value('parentid', -1);

        if ($count == 0) return '休假中';

        $status = '';
        if ($count % 2 == 0)
            $status = '休息中';
        else if ($count % 2 == 1)
            $status = $pid == 6 ? '学习中' : '工作中';

        return $status;
    }

    /**
     * 根据用户Id返回当前用户今天工作状态
     * @param $id
     * @return string
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getUserWorkStatusById($id)
    {
        $data = self::get($id);
        $ret = self::workStatus($data['count'], $data['dept']);
        return $ret;
    }

    /**
     * 返回当天正在工作用户列表
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getOnWorking($where = [])
    {
        $ret = self::where($where)->field('id,avatar,name,dept')->select();
        return $ret;
    }

    public static function getList($where, $field)
    {
        $rows = empty(input('rows')) ? config('paginate.list_rows') : input('rows');
        $list = self::where($where)->field($field)->order('code')->paginate($rows);
        $ret = $list->render();
        return $ret;
    }
}